package copyengine.ui.component.scrollablePanel.element.list
{
    import copyengine.ui.component.scrollablePanel.element.list.displayContainerSharePool.ICEListElementDisplayContainerSharePool;
    import copyengine.ui.component.scrollablePanel.scrollPanel.CEScrollPanel;
    import copyengine.ui.infoTree.buildTree.InfoTreeBuildResult;

    import flash.geom.Rectangle;

    import starling.display.DisplayObject;
    import starling.display.DisplayObjectContainer;

    public class CEInfoTreeListElementBasic implements ICEListElement
    {
        protected var mSortRank:int;
        protected var mRenderData:*;
        protected var mSharePool:ICEListElementDisplayContainerSharePool;
        protected var mElementBounds:Rectangle;
        protected var mScrollPanel:CEScrollPanel;

        protected var mCurrentBuildResult:InfoTreeBuildResult;

        public function CEInfoTreeListElementBasic()
        {
            mElementBounds = new Rectangle();
        }

        /**
         * 每一个Element对象都会有一个RenderData,这个是一对一的
         */
        public function set renderData(_data:*):void
        {mRenderData = _data;}

        public function get renderData():* {return mRenderData;}

        public function set displayContainerSharePool(_pool:ICEListElementDisplayContainerSharePool):void
        {
            mSharePool = _pool;
            mElementBounds.x = mElementBounds.y = 0;
            mElementBounds.width = mSharePool.getContainerBounds().width;
            mElementBounds.height = mSharePool.getContainerBounds().height;
        }

        public function get elementContainer():DisplayObject {return mCurrentBuildResult.rootMc;}

        public function getBounds():Rectangle {return mElementBounds;}

        public function get sortRank():int {return mSortRank;}

        public function set sortRank(_newRank:int):void {mSortRank = _newRank;}

        /**
         * Show函数会被多次调用
         * 1` 在当前Element被刚刚显示的时候(由Hide变为Show)
         * 2` 在移动的时候(本身已经Show了,只不过位置发生了移动)
         */
        public final function show(_scrollPanelContainer:DisplayObjectContainer, _viewportRe:Rectangle):void
        {
            var elementContainer:DisplayObject;
            if (mCurrentBuildResult == null) {
                mCurrentBuildResult = mSharePool.getFreeITBuildResult();
                elementContainer = mCurrentBuildResult.rootMc;
                if (elementContainer.parent != _scrollPanelContainer) {
                    _scrollPanelContainer.addChild(elementContainer);
                }
                elementContainer.visible = true;
                //子类只在由Hide变为Show的时候被调用一次 执行本次显示的初始化逻辑
                //当在显示中位置发生移动的话不再回调子类,如果后续有需要可以提供doMove()方法
                doShow(_scrollPanelContainer, _viewportRe);
            } else {
                elementContainer = mCurrentBuildResult.rootMc;
            }
            elementContainer.x = mElementBounds.x - _viewportRe.x;
            elementContainer.y = mElementBounds.y - _viewportRe.y;
        }

        public final function hide():void
        {
            //在ScrollPanel refresh()的时候会让所有没有显示的Element均执行一遍Hide函数
            //有些Element可能此时还未显示出来,所以这里要做一次判断,只有那些已经显示了的Element才会
            //调用子类的doHide方法
            if (mCurrentBuildResult != null) {
                doHide();
                mSharePool.returnITBuildResult(mCurrentBuildResult);
                mCurrentBuildResult = null;
            }
        }

        public function set scrollPanel(_scrollPanel:CEScrollPanel):void {mScrollPanel = _scrollPanel;}

        public function setInScrollPanelPos(_x:int, _y:int):void
        {
            mElementBounds.x = _x;
            mElementBounds.y = _y;
        }

        public function dispose():void
        {
            //ElementDispose时候只是归还显示对象 不是销毁
            //显示对象由SharePool统一销毁
            if (mCurrentBuildResult != null) {
                mSharePool.returnITBuildResult(mCurrentBuildResult);
                mCurrentBuildResult = null;
            }
        }

        //==========================//
        //==Protected  Function
        //==========================//

        protected function doShow(_scrollPanelContainer:DisplayObjectContainer, _viewportRe:Rectangle):void
        {}

        protected function doHide():void {}
    }
}
